using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 部门管理实体
    /// 对应 departments 表，支持层级组织架构和多租户隔离
    /// </summary>
    [Table("departments")]
    public class Department : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// 用于多租户数据隔离
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;
        
        /// <summary>
        /// 部门名称
        /// </summary>
        [Required]
        [StringLength(255)]
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 部门代码
        /// </summary>
        [StringLength(50)]
        public string? Code { get; set; }

        /// <summary>
        /// 父部门ID（层级结构）
        /// </summary>
        public long? ParentId { get; set; }

        /// <summary>
        /// 部门描述
        /// </summary>
        [Column(TypeName = "text")]
        public string? Description { get; set; }

        /// <summary>
        /// 部门负责人
        /// </summary>
        public long? ManagerId { get; set; }

        /// <summary>
        /// 排序权重
        /// </summary>
        public int SortOrder { get; set; } = 0;

        /// <summary>
        /// 状态（启用/禁用）
        /// 0=禁用, 1=启用
        /// </summary>
        public byte Status { get; set; } = 1;

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 父部门导航属性
        /// </summary>
        [ForeignKey(nameof(ParentId))]
        public Department? Parent { get; set; }

        /// <summary>
        /// 子部门集合
        /// </summary>
        [InverseProperty(nameof(Parent))]
        public ICollection<Department> Children { get; set; } = new List<Department>();

        /// <summary>
        /// 部门负责人导航属性
        /// </summary>
        [ForeignKey(nameof(ManagerId))]
        public User? Manager { get; set; }

        /// <summary>
        /// 该部门下的用户集合
        /// </summary>
        [InverseProperty(nameof(User.Department))]
        public ICollection<User> Users { get; set; } = new List<User>();
    }
}
